package hhm.android.account

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import siau.android.base.SBApplication
import siau.android.base.SystemUtil
import siau.android.http.model.*
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import siau.android.base.HttpHelper
import java.util.concurrent.TimeUnit

class LoginViewModel : ViewModel() {

    var errorString = MutableLiveData<String>()
    val PASSWORD_LOGIN = 1
    val VERI_CODE_LOGIN = 2
    val WX_LOGIN = 3
    var loginType = MutableLiveData(VERI_CODE_LOGIN)
    var canGetCode = false
    var hasGetCode = MutableLiveData(false)
    var showPassword = false
    var canLogin = MutableLiveData(false)
    var countDownNumber = MutableLiveData(60)
    var loginLiveData = MutableLiveData<LoginResponse>()
    var dataLiveData = MutableLiveData<GetDefaultFamilyResponse>()
    var wxLoginLiveData = MutableLiveData<WXLoginResponse>()
    private var subscribe: Disposable? = null
    var wxInfoId: Int? = null

    fun startCountDown(o: Observable<ActivityEvent>, phone: String) {
        subscribe = Observable.interval(0,1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
            val t = 59 - it.toInt()
            if (t == 0) {
                countDownNumber.value = 60
                hasGetCode.value = false
                subscribe?.dispose()
            } else {
                countDownNumber.value = t
            }
        }

        val request = MobileRequest(phone)
        HttpHelper().getCaptcha(request)?.compose(RxLifecycle.bindUntilEvent(o, ActivityEvent.STOP))?.subscribe({}, {
            errorString.value = it.message
        })
    }

    fun passwordLogin(o: Observable<ActivityEvent>, mobile: String, password: String) {
        val request = PasswordLoginRequest(
            mobile, password, SystemUtil.getAndroidSystem(), "", SystemUtil.getSystemModel(), SystemUtil.getSystemVersion(), SBApplication.longitude, SBApplication.latitude
        )
        HttpHelper().passwordLogin(request)?.compose(RxLifecycle.bindUntilEvent(o, ActivityEvent.STOP))?.subscribe({
            loginLiveData.value = it
        }, {
            errorString.value = it.message
        })
    }

    fun smsLogin(o: Observable<ActivityEvent>, mobile: String, code: String) {
        val request = SmsLoginRequest(
            mobile, code, SystemUtil.getAndroidSystem(), "", SystemUtil.getSystemModel(), SystemUtil.getSystemVersion(), SBApplication.longitude, SBApplication.latitude
        )
        if (loginType.value == WX_LOGIN) {
            request.wxInfoId = wxInfoId
        }
        HttpHelper().smsLogin(request)?.compose(RxLifecycle.bindUntilEvent(o, ActivityEvent.STOP))?.subscribe({
            loginLiveData.value = it
        }, {
            errorString.value = it.message
        })
    }

    fun getDefaultFamily(o: Observable<ActivityEvent>) {
        HttpHelper().getDefaultFamily()?.compose(RxLifecycle.bindUntilEvent(o, ActivityEvent.STOP))?.subscribe({
            dataLiveData.value = it
        }, {
            errorString.value = it.message
        })
    }

    fun finish() {
        subscribe?.dispose()
    }

    fun wxLoginData(code: String) {
        val r = WXLoginRequest(code)
        HttpHelper().getWXInfo(r)?.subscribe({
            wxLoginLiveData.value = it
        }, {
            errorString.value = it.message
        })
    }
}